<?php

class product_model extends FlexiGrid_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    function get($ID=""){
        $where=array("ProductID"=>$ID,"Delete"=>Null);
        $query=$this->db->get_where('dtb_product', $where); 
        return $query->result();
    }
    function getRand(){
        $where=array("Delete"=>Null);
        
        $query=$this->db
                ->order_by('ProductID', 'RANDOM')
                ->get_where('dtb_product', $where,1); 
        return $query->result();
    }
    function getRand10(){
        $query=$this->db
                ->query("SELECT GROUP_CONCAT( ProductID ) as dis FROM (SELECT  dtb_product.ProductID FROM dtb_product order by rand() LIMIT 10) d");
        return $query->result();
    }
    function getByAlias($Alias=""){
        $where=array("Alias"=>$Alias,"Delete"=>Null);
        $query=$this->db->get_where('dtb_product', $where); 
        return $query->result();
    }
    function getIn($list=array()){
        $where=array("Delete"=>null);
        
        $query=$this->db
                ->where_in('ProductID', $list)
                ->where($where)
                ->get('dtb_product'); 
        return $query->result();
    }
    function gets($page=0,$ca=""){
        $limit=6;
        $start=$page*$limit;
        
        $this->db
            ->where(array(
                "Delete"=>NULL
                ,"Discontinued"=>"0"
                ));
        if($ca!=""){
           $this->db
            ->where("Categorys",$ca); 
        }
        $query=$this->db
                ->get('dtb_product',$limit,$start); 
        return $query->result();
    }
    function like($ID){
        $this->db->set('`Like`', "`Like` + 1",false)
            ->where('ProductID', $ID)
            ->update('dtb_product'); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function dislike($ID){
        $this->db->set('`Dislike`', "`Dislike` + 1",false)
            ->where('ProductID', $ID)
            ->update('dtb_product'); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
}
?>
